import pandas as pd
import time
import os
import shutil
from split_utils import split_author, cleaning_msg
from split_utils import rule_list_1
from process.split_1 import split_1
from process.split_2 import split_2
from process.pick_1 import pick_1


if __name__ == '__main__':

    # 使用说明 -----------------------------------
    time.sleep(1)
    print('===========================================================')
    print('简单说明：使用正则表达式拆分excel表中不规范的作者，初步提取对应需求字段')
    print('PS:')
    print('1.文件夹下需要有一个excel（只放一个，名称随意），其中一列“作者”保存着待拆分的作者')
    print('2.拆分后的excel将新增几列拆分结果列，以 <作者>[拆分] 作为列名标记')
    print('===========================================================')
    time.sleep(1)
    # ------------------------------------------

    # 输入拆分规则 --------------------------
    print('功能选择：')
    print('1.【拆分】作者拆分，拆分符号  {}'.format(rule_list_1[0]))
    # print('0.题名提取')
    print('2.【拆分】文本加标点的特殊拆分方法')
    print('3.【提取】提取“拆分后元数据内容”到对应字段')
    # print('4.【提取】提取“未匹配到的数据”对字段进行补充（人工处理时使用）')
    # print('5.【归纳】对提取后的字段进行具体的归纳')

    rule_num = input('请选择执行功能：')
    rule_num = int(rule_num)
    # -------------------------------------

    # 输入操作路径 ----------------------------------------------------------------
    operate_dir = input('请输入excel目录（旺柴）:')  # D:\PycharmProjects\spiders\图片下载工具\excel
    operate_dir = os.path.abspath(operate_dir)
    # operate_dir = os.path.abspath(r'C:\Users\cxstar46\Desktop\正则表达式题名拆分测试')
    # -----------------------------------------------------------------------------

    # 读取配置文件 --------------------------------------
    # 只有功能1读取“原题名”存入“拆分后元数据内容”，其他程序都是读取“拆分后元数据内容”存入“拆分后元数据内容”
    if rule_num == 1:
        split_1(operate_dir)
    elif rule_num == 2:
        split_2(operate_dir)
    elif rule_num == 3:
        pick_1(operate_dir)
    # elif rule_num == 4:
    #     pick_2(operate_dir)
    # elif rule_num == 5:
    #     classify_1(operate_dir)
    else:
        print('命令输入有误！')
        input('按任意键退出...')
        raise Exception('123')
    # -------------------------------------------------

    input("按任意键退出...")














    #
    #
    #
    # # 读取配置文件 --------------------------------------
    # source_title_col = '作者'
    # # -------------------------------------------------
    #
    # # 检测excel数量，只能放一个，当只有一个excel时，提取它的路径excel_path -------
    # print('检查路径下的文件格式...')
    # excel_name = None
    # excel_count = 0
    # file_list = os.listdir(operate_dir)
    # for file in file_list:
    #     if file.endswith('.xlsx') or file.endswith('.xlx'):
    #         excel_count += 1
    #         excel_name = file
    # if excel_count == 0:
    #     input('文件夹下没有excel，按任意键退出...')
    #     raise Exception(0)
    # if excel_count > 1:
    #     input('只能放一个excel，按任意键退出...')
    #     raise Exception(0)
    #
    # # print(excel_name)
    # # raise Exception(1212)
    # # ----------------------------------------------------------------------
    # # print(excel_path)
    # # print(img_dir)
    #
    # # 读取excel ----------------------------------------
    # excel_path = os.path.join(operate_dir, excel_name)
    # # print(excel_path)
    # try:
    #     df = pd.read_excel(excel_path)
    #     df = df.where(df.notnull(), None)
    # except Exception as e:
    #     print(e)
    #     print('文件不存在或已损坏...')
    #     input('按任意键退出...')
    #     raise Exception('123')
    # # -------------------------------------------------
    #
    # # 备份原始excel表 --------------------------------------------------------------
    # print('备份excel...')
    # bak_dir = '作者拆分前的备份'
    # file_list = os.listdir(operate_dir)
    # if bak_dir not in file_list:
    #     os.makedirs(os.path.join(operate_dir, bak_dir))
    # bak_excel_path = os.path.join(os.path.join(operate_dir, bak_dir), excel_name)
    # shutil.copyfile(excel_path, bak_excel_path)
    # # -----------------------------------------------------------------------------
    #
    # # excel_path = r'./15.2、上海交通大学出版社详情-含封面.xlsx'
    # df = pd.read_excel(excel_path)
    # df = df.where(df.notnull(), None)
    #
    # # --------------------------------
    # all_nomatch_list = []  # 未匹配到的数据
    # all_no_split_author_flag = []   # 未被拆分的作者标记为True
    # all_countryside_list = []  # 国别/年代
    # all_name_list = []  # 姓名
    # all_zlfs_list = []  # 著录方式
    # all_alias_list = []  # 别名
    # # --------------------------------
    #
    # for i in range(df.shape[0]):
    #
    #     # 源作者
    #     try:
    #         author = str(df.loc[i, '作者'])
    #     except Exception as e:
    #         print(e)
    #         print('excel中好像没有“作者”这一列')
    #         input('按任意键退出...')
    #         raise Exception('123')
    #
    #     print(i, author)
    #
    #     author = cleaning_msg(author)
    #     split_list = split_author(author)
    #     # print(split_list)
    #     # split_list = ASOP.clean_split_list(split_list)
    #     print(split_list)
    #
    #     # 没有被拆分的原题名标记 ---------
    #     if len(split_list) <= 1:
    #         no_split_author_flag = True
    #     else:
    #         no_split_author_flag = False
    #     all_no_split_author_flag.append(no_split_author_flag)
    #     # ----------------------------
    #
    #     # 国别/年代
    #     countryside_list = AuthorPickOp.countryside(split_list)
    #     print('countryside_list： {}'.format(countryside_list))
    #     countryside_str = ';'.join(countryside_list) if countryside_list else None
    #     all_countryside_list.append(countryside_str if not no_split_author_flag else None)
    #
    #     # 姓名
    #     name_list = AuthorPickOp.name(split_list)
    #     print('name_list： {}'.format(name_list))
    #     name_str = ';'.join(name_list) if name_list else None
    #     all_name_list.append(name_str if not no_split_author_flag else None)
    #
    #     # 别名
    #     alias_list = AuthorPickOp.alias(split_list)
    #     alias_str = ';'.join(alias_list) if alias_list else None
    #     all_alias_list.append(alias_str if not no_split_author_flag else None)
    #
    #     # 著录方式
    #     zlfs_list = AuthorPickOp.zlfs(split_list)
    #     print('zlfs_list： {}'.format(zlfs_list))
    #     zlfs_str = ';'.join(zlfs_list) if zlfs_list else None
    #     all_zlfs_list.append(zlfs_str if not no_split_author_flag else None)
    #
    #     # 未匹配到的数据提取 --------------------------------------------
    #     nomatch_list = []
    #     match_list = countryside_list + name_list + zlfs_list + alias_list
    #     for txt in split_list:
    #         if txt not in match_list:
    #             nomatch_list.append(txt)
    #     nomatch_str = ';'.join(nomatch_list) if nomatch_list else None
    #     all_nomatch_list.append(nomatch_str if not no_split_author_flag else None)
    #     # -------------------------------------------------------------
    #
    # # 打印拆分结果 ------------------
    # print('all_no_split_author_flag: {}'.format(all_no_split_author_flag))
    # print('all_countryside_list: {}'.format(all_countryside_list))
    # print('all_name_list: {}'.format(all_name_list))
    # print('all_zlfs_list: {}'.format(all_zlfs_list))
    # print('all_alias_list: {}'.format(all_alias_list))
    # # -----------------------------
    #
    # # 保存拆分结果 ----------------------------
    # df.loc[:, '[拆分]未匹配到的数据'] = all_nomatch_list
    # df.loc[:, '[拆分]国籍和年代'] = all_countryside_list
    # df.loc[:, '[拆分]作者名称'] = all_name_list
    # df.loc[:, '[拆分]作者其他名称'] = all_alias_list
    # df.loc[:, '[拆分]著录方式'] = all_zlfs_list
    # df.loc[:, '[拆分]作者未拆分标记'] = all_no_split_author_flag
    #
    # print('正在保存...')
    # df.to_excel(excel_path, index=0)
    # print('保存完成')
    # # ---------------------------------------备份excel...
    #
    # input("按任意键退出...")